package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.LiveClassifyInfos;
import com.elinkway.infinitemovies.bean.LiveTvStationDetail;
import com.elinkway.infinitemovies.config.LogoImageLoader;
import com.le123.ysdq.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LiveVideoListAdapter extends MyBaseAdapter{
	private LiveClassifyInfos mData;
	private SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm");
    public LiveVideoListAdapter(Context context, LiveClassifyInfos data) {
    	
        super(context);
        mData = data;
    }

    @Override
    public int getCount() {
        return mData.getmChannels().size();
    }
    
    private class ItemHolder {
        ImageView channelIcon;
        ImageView playingIcon;

        TextView channelName;
        TextView videoTime1;
        TextView videoName1;
        TextView videoTime2;
        TextView videoName2;
        TextView noVideoInfo;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemHolder itemHolder = null;
        LiveTvStationDetail detail = mData.getmChannels().get(position);
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.listmodel_liveinfo, parent, false);
            itemHolder = new ItemHolder();
            itemHolder.channelIcon = (ImageView) convertView.findViewById(R.id.live_icon);
            itemHolder.playingIcon = (ImageView) convertView.findViewById(R.id.live_nowplaying);

            itemHolder.channelName = (TextView) convertView.findViewById(R.id.liveicon_name);
            itemHolder.videoName1 = (TextView) convertView.findViewById(R.id.playname1);
            itemHolder.videoTime1 = (TextView) convertView.findViewById(R.id.playtime1);
            itemHolder.videoName2 = (TextView) convertView.findViewById(R.id.playname2);
            itemHolder.videoTime2 = (TextView) convertView.findViewById(R.id.playtime2);
            itemHolder.noVideoInfo = (TextView) convertView.findViewById(R.id.no_videoinfo);

            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }
        itemHolder.channelName.setText(detail.getName());
        LogoImageLoader.getInstance().displayImage(detail.getIcon(), itemHolder.channelIcon);
        if(detail.getPlayTimeList().size()>0){
        	itemHolder.playingIcon.setVisibility(View.VISIBLE);
        	itemHolder.videoName1.setVisibility(View.VISIBLE);
            itemHolder.noVideoInfo.setVisibility(View.GONE);
        	itemHolder.videoName1.setText(detail.getPlayTimeList().get(0).getTitle());
        	itemHolder.videoTime1.setVisibility(View.VISIBLE);
        	itemHolder.videoTime1.setText(convertTime(detail.getPlayTimeList().get(0).getPlayTime()));
        	if(detail.getPlayTimeList().size()>1){
            	itemHolder.videoName2.setVisibility(View.VISIBLE);
            	itemHolder.videoName2.setText(detail.getPlayTimeList().get(1).getTitle());
            	itemHolder.videoTime2.setVisibility(View.VISIBLE);
            	itemHolder.videoTime2.setText(convertTime(detail.getPlayTimeList().get(1).getPlayTime()));
        	}else{
                itemHolder.videoName2.setVisibility(View.INVISIBLE);
                itemHolder.videoTime2.setVisibility(View.INVISIBLE);
        	}
        }else{
            itemHolder.videoName1.setVisibility(View.GONE);
            itemHolder.videoName2.setVisibility(View.GONE);
            itemHolder.videoTime1.setVisibility(View.GONE);
            itemHolder.videoTime2.setVisibility(View.GONE);
        	itemHolder.playingIcon.setVisibility(View.GONE);
            itemHolder.noVideoInfo.setVisibility(View.VISIBLE);
        }
        return convertView;
    }
    private String convertTime(String time){
    	try {
			Date time1 = dateParser.parse(time);
			return dateFormatter.format(time1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return "";
    }
}
